package com.example.springboot.controller;


import com.example.springboot.pojo.WorkOrder;
import com.example.springboot.service.EquipmentServiceImpl;
import com.example.springboot.service.WorkOrderServiceImpl;
import com.example.springboot.util.Page;
import org.apache.tomcat.util.net.jsse.JSSEUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/WorkOrder")
public class WorkOrderController {
    @Autowired
    @Qualifier("workOrderService")
    WorkOrderServiceImpl workOrderService;
    @Autowired
    @Qualifier("equipmentService")
    EquipmentServiceImpl equipmentService;
    @RequestMapping("/addWorkOrder")
    public String addWorkOrder(Model model, WorkOrder workOrder,HttpSession session) {
        System.out.println("当前工单是=====" + workOrder);
        boolean flag = workOrderService.addWorkOrder(workOrder);
        model.addAttribute("flag", flag);
        return "redirect:/WorkOrder/getWorkOrderList/"+session.getAttribute("currentPage");
    }
    @RequestMapping("/showAddWorkOrder")
    public String showAddWorkshop(Model model) {
        return "addWorkOrder";
    }
    @RequestMapping("/getWorkOrderList/{pageNum}")
    public String getWorkOrderList(Model model, HttpSession session, @PathVariable int pageNum) {
        int id= (int) session.getAttribute("eqid");
        List<WorkOrder> workOrderList = equipmentService.getWorkOrderListById(id);
        Page<WorkOrder> page = new Page<>(pageNum, 8, workOrderList);
        model.addAttribute("page", page);
        System.out.println(page.dataList);
        session.setAttribute("currentPage", page.currentPage);
        return "workOrderList";
    }

    @RequestMapping("/queryById/{id}")
    public void queryById(Model model, @PathVariable int id) {
        WorkOrder workOrder = workOrderService.queryById(id);
        model.addAttribute("workOrder", workOrder);
    }

    @RequestMapping("/deleteById/{id}")
    public String deleteById(Model model, @PathVariable int id,HttpSession session) {
        boolean flag = workOrderService.deleteById(id);
        model.addAttribute("flag", flag);
        return "redirect:/WorkOrder/getWorkOrderList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/updateById/{id}")
    public String updateById(Model model, WorkOrder workOrder,@PathVariable int id,HttpSession session) {
        workOrder.setId(id);
        boolean flag = workOrderService.updateById(workOrder);
        model.addAttribute("flag", flag);
        return "redirect:/WorkOrder/getWorkOrderList/"+session.getAttribute("currentPage");
    }

    @RequestMapping("/showUpdateWorkOrder/{id}")
    public String showUpdateWorkOrder(Model model,@PathVariable int id){
        WorkOrder workOrder = workOrderService.queryById(id);
        model.addAttribute("workOrder",workOrder);
        return "updateWorkOrder";
    }



    @RequestMapping("/getWorkOrderListByCondition/{pageNum}")
    public String getWorkOrderListByCondition(Model model,@PathVariable int pageNum,String name,String workshop_id,HttpSession session){
        Map<String,String> map=new HashMap<>();
        if(name.isEmpty())
            map.put("name",null);
        else
            map.put("name",name);
        if(workshop_id.isEmpty())
            map.put("workshop_id",null);
        else
            map.put("workshop_id",workshop_id);
        List<WorkOrder> workOrderList = workOrderService.getWorkOrderListByCondition(map);
        session.setAttribute("womap",map);
        Page<WorkOrder> page = new Page<>(pageNum, 8, workOrderList);
        model.addAttribute("page", page);
        return "workOrderListByCon";
    }
    @RequestMapping("/getWorkOrderListByCondition2/{pageNum}")
    public String getWorkOrderListByCondition2(Model model,@PathVariable int pageNum,HttpSession session){
        Map<String,String> map= (Map<String, String>) session.getAttribute("womap");
        List<WorkOrder> workOrderList = workOrderService.getWorkOrderListByCondition(map);
        Page<WorkOrder> page = new Page<>(pageNum, 8, workOrderList);
        model.addAttribute("page", page);
        return "workOrderListByCon";
    }
}
